//https://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9?tpId=13&tqId=1377477&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
#include <list>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* FindKthToTail(ListNode* pHead, int k)
    {
        // // write code here
        // ListNode* nul = nullptr;
        // if(pHead == nullptr)
        //     return nullptr;

        // ListNode* cur = pHead;

        // int count = 0;
        // while(pHead)
        // {
        //     count++;
        //     pHead = pHead->next;
        // }

        // if(k > count)
        // {
        //     return nul;
        // }

        // while(cur)
        // {
        //     if(count != k)
        //     {
        //         cur = cur->next;
        //         count--;
        //     }
        //     return cur;
        // }

        ListNode* fast = pHead;
        ListNode* slow = pHead;

        if(pHead == nullptr)
        {
            return nullptr;
        }

        if(k == 0)
        {
            return slow = nullptr;
        }    

        for(int i = 0; i < k; i++)
        {
            if(fast != nullptr)
            {
                fast = fast->next;
            }
            else 
            {
                return nullptr;
            }
        }

        while(fast)
        {
            fast = fast->next;
            slow = slow->next;
        }

        return slow;
    }
};